<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/3/21
  Time: 11:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
      <script src="static/js/jquery-3.6.0.min.js"></script>
  </head>
  <body>
 <%-- <%
    //设置不同范围的值
    pageContext.setAttribute("pageKey",1000);
    request.setAttribute("requestKey",2000);
    session.setAttribute("seessionKey",3000);
    application.setAttribute("applicationKey",4000);
   /* request.getRequestDispatcher("success.jsp").forward(request,response);*/
    response.sendRedirect("success.jsp");
  %>--%>
  <h1>用户登录</h1>
    <form action="login" method="post">
      <input type="text" name="username" value="admin"><br>
      <input type="text" name="password" value="123456"><br>
      <input type="submit" value="登录">
    </form>
  <hr>
    <input type="text" id="id" onblur="getUserName()"><br>
  <button onclick="getUserName()">测试ajax</button><br>
  结果：<input type="text" id="username">
  </body>
    <script>
            function getUserName() {
                let byId = document.getElementById("id").value;
                $.ajax({
                    url:"userinfo",//路径
                    data:{method:"findUserNameById",id:byId},//传递给后台的参数
                    dataType:"JSON",//相应的数据类型：服务器给我的
                    type:"POST",
                    async:false,
                    success:function (dataInfo) {
                     /*   alert(dataInfo.username)
                        //成功的响应函数  ：dataInfo名字随便写，代表的是服务器相应的内容
                        console.log(dataInfo.username)*/
                     document.getElementById("username").value=dataInfo.username;
                    }
                })
            }
       /* //获取xmlHttpRequest对象
        function createXHR(){
            try {
                return new XMLHttpRequest();
            } catch (e) {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        function getUserName() {
            //1、获取xmlHttpRequest对象
            let xhr = createXHR();
            //2、设置请求格式
            let byId = document.getElementById("id").value;
            xhr.open("GET","userinfo?method=findUserNameById&id="+byId,true);
            //3、设置回调函数
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        var obj = document.getElementById("username");
                        obj.value = xhr.responseText;
                    } else {
                        alert("AJAX服务器返回错误！");
                    }
                }
            }
            //4、发送请求
            xhr.send(null);
        }*/
    </script>
</html>
